//
//  HWHomePageMainListTableViewCell.m
//  HappyWeekend
//
//  Created by lanou on 16/1/13.
//  Copyright © 2016年 com.mjykj.www. All rights reserved.
//

#import "HWHomePageMainListTableViewCell.h"
#define kMargin 10
#define kEdge 2
#define kLabelH 20


@implementation HWHomePageMainListTableViewCell


- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    self = [super initWithStyle:style reuseIdentifier:reuseIdentifier];
    if (self) {
     
        self.timeLabel = [[UILabel alloc]initWithFrame:CGRectMake(kMargin, kEdge, kScreenWidth - kMargin, kLabelH)];
        [self.contentView addSubview:_timeLabel];
        
        self.coverImageView = [[UIImageView alloc] initWithFrame:CGRectMake(kEdge, _timeLabel.bottom + kEdge, kScreenWidth - kEdge * 2, kScreenWidth / 600 * 275)];
        [self.contentView addSubview:_coverImageView];
        
        self.likeButton = [[HWIconLabelButton alloc] initWithFrame:CGRectMake(kScreenWidth - 65, kEdge, 55, 20)];
        [_coverImageView addSubview:_likeButton];
        
        self.titelLabel = [[UILabel alloc] initWithFrame:CGRectMake(kEdge, _coverImageView.height - 50, _coverImageView.width, 50)];
        [_coverImageView addSubview:_titelLabel];
        
    }
    return self;
}


- (void)setModel:(HWHomePageMainModel *)model
{
    _model = model;
    [self.coverImageView sd_setImageWithURL:[NSURL URLWithString:model.cover_image_url]];
    NSDate *date = [NSDate dateWithTimeIntervalSince1970:[model.created_at integerValue]];
    NSString *someDay = [self weekdayStringFromDate:date];
    self.timeLabel.text = [[[NSString stringWithFormat:@"%@",date] substringToIndex:10] stringByAppendingString:someDay];
    self.likeButton.likeLabel.text = [NSString stringWithFormat:@"%@",model.likes_count];
    self.likeButton.likeLabel.font = [UIFont boldSystemFontOfSize:12];
    self.likeButton.likeLabel.textColor = [UIColor whiteColor];
    self.likeButton.layer.cornerRadius = 8;
    self.likeButton.backgroundColor = [UIColor colorWithRed:0.f green:0.f blue:0.f alpha:0.5];
    self.titelLabel.text = model.title;
    self.titelLabel.numberOfLines = 0;
    self.titelLabel.textColor = [UIColor whiteColor];
    self.titelLabel.font = [UIFont boldSystemFontOfSize:16];
}

- (NSString *)weekdayStringFromDate:(NSDate*)inputDate
{
    NSArray *weekdays = [NSArray arrayWithObjects: [NSNull null], @"周日", @"周一", @"周二", @"周三", @"周四", @"周五", @"周六", nil];
    
    NSCalendar *calendar = [[NSCalendar alloc] initWithCalendarIdentifier:NSCalendarIdentifierGregorian];
    
    NSTimeZone *timeZone = [[NSTimeZone alloc] initWithName:@"Asia/Shanghai"];
    
    [calendar setTimeZone: timeZone];
    
    NSCalendarUnit calendarUnit = NSCalendarUnitWeekday;
    
    NSDateComponents *theComponents = [calendar components:calendarUnit fromDate:inputDate];
    
    return [weekdays objectAtIndex:theComponents.weekday];

}



- (void)awakeFromNib {
    // Initialization code
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
